package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Classroom;
import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Grade;
import cn.wolfcode.crm.mapper.ClassroomMapper;
import cn.wolfcode.crm.mapper.GradeMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClassroomService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ClassroomServiceImpl implements IClassroomService {

    @Autowired
    private ClassroomMapper classroomMapper;
    @Autowired
    private GradeMapper gradeMapper;

    //保存和更新教室操作
    public int saveOrUpdate(Classroom record) {
        if (record.getId() == null) {
            return classroomMapper.insert(record);
        } else {
            return classroomMapper.updateByPrimaryKey(record);
        }
    }

    //查询单个教室操作
    public Classroom selectByPrimaryKey(Long id) {
        return classroomMapper.selectByPrimaryKey(id);
    }


    //教室列表操作
    public List<Classroom> selectAll() {
        return classroomMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = classroomMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Classroom> data = classroomMapper.queryList(qo);
        return new PageResult(count, data);
    }

    //改变教室的状态
    public void changeState(Long id) {
        classroomMapper.changeState(id);
    }

    public Boolean validateName(String name, Long id) {
        //接受传入的教室名字
        Classroom classroom = classroomMapper.validateName(name);
        //如果classroom找不到 教室名字不存在   验证通过 返回true
        //如果前台传过来的id 和数据库查询到id相同 则是相同的用户 在做编辑操作的时候 验证也通过 返回true
        return classroom == null || classroom.getId() == id;
    }


}
